/*
 * @(#)HtmlDynamicResultTestCase.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.result;

import junit.framework.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import java.util.HashMap;
import java.util.Map;

import com.nhncorp.lucy.common.config.model.ApplicationInfo;
import com.nhncorp.lucy.web.helper.ServletHelper;
import com.nhncorp.usf.core.config.ReplaceAllInfo;
import com.nhncorp.usf.core.config.runtime.ResultPageInfo;
import com.nhncorp.usf.core.config.runtime.ResultPageInfo.DISPATCHER_TYPE;
import com.nhncorp.usf.core.result.direct.HtmlDynamicResult;
import com.nhncorp.usf.core.util.StringUtil;

import com.opensymphony.xwork.ActionContext;

/**
 * {@link com.nhncorp.usf.core.result.direct.HtmlResult} TestCase.
 *
 * @author Web Platform Development Team
 */
public class HtmlDynamicResultTestCase {
    AbstractResult result = new HtmlDynamicResult();
    //ResultPageInfo resultPageInfo;

    @BeforeClass
    public static void initialize() throws Exception {
        ApplicationInfo.setPageEncoding("utf-8");
    }

    @Before
    public void init() {
        MockHttpServletResponse response = new MockHttpServletResponse();
        MockHttpServletRequest request = new MockHttpServletRequest();
        ActionContext.getContext().put(ServletHelper.HTTP_REQUEST, request);
        ActionContext.getContext().put(ServletHelper.HTTP_RESPONSE, response);
    }

    @Test
    public void executeScriptResult() throws Exception {
        ResultPageInfo resultPageInfo = new ResultPageInfo(DISPATCHER_TYPE.direct, "<script>name=${name}; age=${age}; parent.addNewStudy();</script>");
        resultPageInfo.setId("dummy_id");
        resultPageInfo.getProperties().setProperty("language", "dynamic");

        Map<String, Object> parameterMap = new HashMap<String, Object>();
        parameterMap.put("name", "choco");
        parameterMap.put("age", "100");

        MockHttpServletResponse response = (MockHttpServletResponse) ServletHelper.getResponse();
        try {
            result.execute(resultPageInfo, parameterMap);
            String contents = response.getContentAsString().trim();
            Assert.assertEquals("<script>name=choco; age=100; parent.addNewStudy();</script>", contents);
        } catch (Exception except) {
            Assert.fail();
        }
    }

    @Test
    public void executeScriptResultWithReplaceAll1() throws Exception {
        String src = "<script>name=${name}; age=${age}; parent.addNewStudy();</script>";
        ResultPageInfo resultPageInfo = new ResultPageInfo(DISPATCHER_TYPE.direct, StringUtil.replacePageSrc(src));
        resultPageInfo.setId("dummy_id");
        resultPageInfo.getProperties().setProperty("language", "dynamic");

        ReplaceAllInfo replaceAllInfo = ReplaceAllInfo.getInstance();
        replaceAllInfo.clear();
        replaceAllInfo.putReplaceInfo("name", "choco-1.0");
        replaceAllInfo.putReplaceInfo("age", "200");

        Map<String, Object> parameterMap = new HashMap<String, Object>();
        parameterMap.put("name", "nhn");
        parameterMap.put("age", "10");
        MockHttpServletResponse response = (MockHttpServletResponse) ServletHelper.getResponse();
        try {
            result.execute(resultPageInfo, parameterMap);
            String contents = response.getContentAsString().trim();
            Assert.assertEquals("<script>name=nhn; age=10; parent.addNewStudy();</script>", contents);
        } catch (Exception except) {
            Assert.fail();
        }
    }

    @Test
    public void executeScriptResultWithReplaceAll2() throws Exception {
        ReplaceAllInfo replaceAllInfo = ReplaceAllInfo.getInstance();
        replaceAllInfo.clear();
        replaceAllInfo.putReplaceInfo("name", "choco-1.0");
        replaceAllInfo.putReplaceInfo("age", "200");

        String src = "<script>name=@{name}; age=@{age}; parent.addNewStudy();</script>";
        ResultPageInfo resultPageInfo = new ResultPageInfo(DISPATCHER_TYPE.direct, StringUtil.replacePageSrc(src));
        resultPageInfo.setId("dummy_id");
        resultPageInfo.getProperties().setProperty("language", "dynamic");

        MockHttpServletResponse response = (MockHttpServletResponse) ServletHelper.getResponse();
        try {
            result.execute(resultPageInfo, new HashMap<String, Object>());
            String contents = response.getContentAsString().trim();
            Assert.assertEquals("<script>name=choco-1.0; age=200; parent.addNewStudy();</script>", contents);
        } catch (Exception except) {
            Assert.fail();
        }
    }

    @Test
    public void executeScriptResultWithReplaceAll3() throws Exception {
        String src = "<script>name=@{choco}; age=@{10}; parent.addNewStudy();</script>";
        ResultPageInfo resultPageInfo = new ResultPageInfo(DISPATCHER_TYPE.direct, StringUtil.replacePageSrc(src));
        resultPageInfo.setId("dummy_id");
        resultPageInfo.getProperties().setProperty("language", "dynamic");

        MockHttpServletResponse response = (MockHttpServletResponse) ServletHelper.getResponse();
        try {
            result.execute(resultPageInfo, new HashMap<String, Object>());
            String contents = response.getContentAsString().trim();
            Assert.assertEquals("<script>name=choco; age=10; parent.addNewStudy();</script>", contents);
        } catch (Exception except) {
            Assert.fail();
        }
    }

    @Test
    public void executeScriptResultWithReplaceAll4() throws Exception {
        ReplaceAllInfo replaceAllInfo = ReplaceAllInfo.getInstance();
        replaceAllInfo.clear();
        replaceAllInfo.putReplaceInfo("name", "choco-1.0");
        replaceAllInfo.putReplaceInfo("age", "100");

        String src = "<script>name=${name}; age=@{age}; parent.addNewStudy();</script>";
        ResultPageInfo resultPageInfo = new ResultPageInfo(DISPATCHER_TYPE.direct, StringUtil.replacePageSrc(src));
        resultPageInfo.setId("dummy_id");
        resultPageInfo.getProperties().setProperty("language", "dynamic");

        Map<String, Object> parameterMap = new HashMap<String, Object>();
        parameterMap.put("name", "nhn");
        parameterMap.put("age", "10");

        MockHttpServletResponse response = (MockHttpServletResponse) ServletHelper.getResponse();
        try {
            result.execute(resultPageInfo, parameterMap);
            String contents = response.getContentAsString().trim();
            Assert.assertEquals("<script>name=nhn; age=100; parent.addNewStudy();</script>", contents);
        } catch (Exception except) {
            Assert.fail();
        }
    }

    @Test
    public void setHeader() throws Exception {
        ResultPageInfo resultPageInfo = new ResultPageInfo(DISPATCHER_TYPE.direct, "<script>parent.addNewStudy();</script>");
        resultPageInfo.setId("dummy_id");
        resultPageInfo.getProperties().setProperty("language", "dynamic");
        resultPageInfo.getProperties().setProperty("header", "HeaderName1 : HeaderValue1 \n HeaderName2 : HeaderValue2");

        MockHttpServletResponse response = (MockHttpServletResponse) ServletHelper.getResponse();
        try {
            result.setHeader(resultPageInfo);
            String header1 = (String) response.getHeader("HeaderName1");
            String header2 = (String) response.getHeader("HeaderName2");

            Assert.assertEquals("HeaderValue1", header1);
            Assert.assertEquals("HeaderValue2", header2);
        } catch (Exception except) {
            Assert.fail();
        }
    }
}